import { Injectable } from '@angular/core';
import { BaseService } from '@core/routes-services/base.service';
import { CacheService } from '@core/routes-services/cache.service';
import { HttpClient } from '@angular/common/http';
import { myValidNull } from '@core/util/string-utils';

/**
 * otc用户 服务
 */
@Injectable()
export class OtcCustomerService extends BaseService {

  constructor(http: HttpClient, cacheService: CacheService) {
    super(http, cacheService);
  }

  base_url: any = 'api/otcCustomers';


  /**
   * 分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  pagination(page: any, keyword: any, role: any, sortName: any, sortValue: any): Promise<any> {
    const url = this.base_url + '/pagination';
    if (keyword) {
      page['_filter_like_name'] = keyword;
    }
    if (role) {
      page['_filter_eq_role'] = role;
    }
    if (myValidNull(sortName) || myValidNull(sortValue)) {
      page['_sort_by_'] = 'lastLoginTime_desc';
    } else {
      page['_sort_by_'] = sortName + '_' + (sortValue == 'ascend' ? 'asc' : 'desc');
    }
    return this.post(url, page);
  }

  /**
   * 选择运营中心的管理员
   *
   * @param user
   * @returns {Promise<any>}
   */
  centerPagination(keyword: any): Promise<any> {
    const url = this.base_url + '/center/options/pagination';
    let page: any = { number: 1, size: 20, totalElements: 0, content: [] };
    if (keyword) {
      page['_filter_like_name'] = keyword;
    }
    return this.post(url, page);
  }

  /**
   * 搜索下拉框
   *
   * @param user
   * @returns {Promise<any>}
   */
  searchOptions(keyword: any): Promise<any> {
    const url = this.base_url + '/pagination';
    let page: any = { number: 1, size: 15, totalElements: 0, content: [] };
    //查 业务经理
    page['_filter_eq_role'] = 2;
    if (keyword) {
      page['_filter_like_name'] = keyword;
    }
    page['_sort_by_'] = 'id_desc';
    return this.post(url, page);
  }

  /**
   * 搜索运营中心业务员下拉框
   *
   * @param user
   * @returns {Promise<any>}
   */
  searchCenterSalesManOptions(centerId:any,keyword: any): Promise<any> {
    const url = this.base_url + '/admin/findByCenterId/'+centerId;
    let page: any = { number: 1, size: 15, totalElements: 0, content: [] };
    //查 业务经理
    page['_filter_eq_role'] = 2;
    if (keyword) {
      page['_filter_like_name'] = keyword;
    }
    page['_sort_by_'] = 'id_desc';
    return this.post(url, page);
  }
  /**
   * 修改
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  saveOrUpdateEntity(id: any, entity: any): Promise<any> {
    if (id) {
      const url = this.base_url + '/' + id;
      return this.put(url, entity);
    } else {
      const url = this.base_url + '/sign_up';
      return this.post(url, entity);
    }
  }

  /**
   * 获取查看详情
   *
   * @param id
   * @returns {Promise<any>}
   */
  detail(id: any): Promise<any> {
    const url = this.base_url + '/' + id;
    return this.get(url);
  }

  /**
   * 根据id获取用户地址详情
   *
   * @param id
   * @returns {Promise<any>}
   */
  cusAddress(id: any): Promise<any> {
    const url = this.base_url + '/actions/address/' + id;
    return this.get(url);
  }

  /**
   * 设置用户管理的运营中心
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  setCenter(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/actions/center/' + id;
    return this.put(url, entity);
  }

  /**
   * 动态管理员设置
   *
   * @param id
   * @param beAuth
   * @returns {Promise<any>}
   */
  setAuth(id: any, beAuth: any): Promise<any> {
    const url = this.base_url + '/actions/auth/' + id + '/' + beAuth;
    return this.put(url, {});
  }
  /**
   * 运营中心管理员设置
   *
   * @param id
   * @param centerManager
   * @returns {Promise<any>}
   */
  setCenterManager(id: any, centerManager: any): Promise<any> {
    const url = this.base_url + '/actions/center/manager/' + id + '/' + centerManager;
    return this.put(url, {});
  }

}
